package com.fancon.android.gardenjokesx.dialog;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.ReportErrors;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;

public class DialogReportJorke extends Dialog {
	private EditText content;
	private Context mContext;
	String photoId, userIdReported;
	Boolean reportUser = false;
	public DialogReportJorke(Context context, String photoId, String userIdReported, Boolean reportUser) {
		super(context);
		mContext = context;
		this.photoId = photoId;
		this.userIdReported = userIdReported;
		this.reportUser = reportUser;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setCancelable(true);
		setContentView(R.layout.m041_report_user);
		Button btnCancel = (Button) findViewById(R.id.m041_report_btn_cancel);
		Button btnSend = (Button) findViewById(R.id.m041_report_btn_send);
		content = (EditText) findViewById(R.id.m041_edit_report_user);

		// Get resion for sending
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Do nothing, just for hide dialog
				dismiss();
			}
		});

		btnSend.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Send the reason report user to server
				if (DialogReportJorke.this.reportUser) {
					doReportUser(content.getText().toString().trim());
				} else {
					doReportPhoto(content.getText().toString().trim());
				}
				
				dismiss();
			}
		});

	}
	
	public void doReportPhoto(String message){
		ReportPhotoRequest report = new ReportPhotoRequest("m04block", (GardenJokesBaseActivity) mContext, message);
		((GardenJokesBaseActivity)mContext).getGlobalState().getRequestQueue().addRequest(report);
	}
	class ReportPhotoRequest extends RequestUI{
		private String message;
		private ReportErrors report;
		private Activity activity;
		public ReportPhotoRequest(Object key, Activity a, String mesage) {
			super(key, a);
			activity = a;
			// TODO Auto-generated constructor stub
			this.message = mesage;
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("user_id_report", UserUtil.getInfoUserLogin(mContext).getUser_id()));
			nameValuePairs.add(new BasicNameValuePair("report_type", "2"));
			nameValuePairs.add(new BasicNameValuePair("report_message", message));
			nameValuePairs.add(new BasicNameValuePair("user_id_reported", userIdReported));
			nameValuePairs.add(new BasicNameValuePair("user_name_reported", DialogReportJorke.this.userIdReported));
			nameValuePairs.add(new BasicNameValuePair("photo_id_reported", photoId));
			nameValuePairs.add(new BasicNameValuePair("photo_name_reported", "photo_name_reported"));
			GardenJokesApiConnect api = new GardenJokesApiConnect(activity);
			String result = api.execPost(mContext, GardenJokesApiConnect.GROUP_M, "report",
					nameValuePairs);
			Log.v("StringResult", result);
			JsonPaser jP = new JsonPaser();
			report = jP.getReport(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			Toast.makeText(mContext, report.getMessage(), 1000);
		}
		
	}
	
	public void doReportUser(String message){
		ReportPhotoRequest report = new ReportPhotoRequest("m04block", (GardenJokesBaseActivity) mContext, message);
		((GardenJokesBaseActivity)mContext).getGlobalState().getRequestQueue().addRequest(report);
	}
	
	class ReportUserRequest extends RequestUI{
		private String message;
		private ReportErrors report;
		public ReportUserRequest(Object key, Activity activity, String mesage) {
			super(key, activity);
			// TODO Auto-generated constructor stub
			this.message = mesage;
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("user_id_report", UserUtil.getInfoUserLogin(mContext).getUser_id()));
			nameValuePairs.add(new BasicNameValuePair("report_type", "1"));
			nameValuePairs.add(new BasicNameValuePair("report_message", message));
			nameValuePairs.add(new BasicNameValuePair("user_id_reported", DialogReportJorke.this.userIdReported));
			nameValuePairs.add(new BasicNameValuePair("user_name_reported", "user_name_reported"));
			nameValuePairs.add(new BasicNameValuePair("photo_id_reported", "photo_id_reported"));
			nameValuePairs.add(new BasicNameValuePair("photo_name_reported", "photo_name_reported"));
			GardenJokesApiConnect api = new GardenJokesApiConnect(mContext);
			String result = api.execPost(mContext, GardenJokesApiConnect.GROUP_M, "report",
					nameValuePairs);
			Log.v("StringResult", result);
			JsonPaser jP = new JsonPaser();
			report = jP.getReport(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			//do alert here
			Toast.makeText(mContext, report.getMessage(), 1000);
		}
		
	}

}
